<template>
  <div>
<el-button @click="closeButton()" size="small" type="primary">导入模板下载</el-button>
    <div style="margin-top:20px;"></div>
    <UploadFile
    action-url="/api/cmscloud.wholesales/ttFiRebateSubprojectsAccount/frostUpload"
    :initial-value="file"
    @change="successHandler"
    :fileTypes="['xlsx','xls']"
  >上传附件</UploadFile>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import {downloadUrl} from '@/utils'
export default {
  data(){
    return{
      urls:'/api/cmscloud.wholesales/ttFiRebate/certificatePostImportFromExcel',
      fetchParams:{

      },
      file:[],
      fetchApi:()=>{},
    }
  },
  methods:{
    closeButton(type){
      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
          confirmButtonText: '下载',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let file = {};
          file.url = 'https://bjev-cms-oss-public.oss-cn-beijing.aliyuncs.com/20211221/16e5d66422f340ebb1c53305803ee408.xlsx';
          file.fileName = '冻结导入';
          this.load(file);

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    load(file) {
      try {
        this.getBlob(file.url).then(blob => {
          this.saveAs(blob, file.fileName);
        });
      } catch (error) {
        console.log(error);
      }
      
    },
    getBlob(url) {
      return new Promise(resolve => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.responseType = 'blob';
        xhr.onload = () => {
          if (xhr.status === 200) {
            resolve(xhr.response);
          }
        };
        xhr.send();
      });
    },
    //下载文件
    saveAs(blob, filename) {
      var link = document.createElement('a');
      link.href = window.URL.createObjectURL(blob);
      link.download = filename;
      link.click();
    },
    successHandler(val){
      console.log(val)
      if(val&&val!=1&&val.length>0&&val[0].url&&val[0].url.errorList&&val[0].url.errorList.length>0){
        const h = this.$createElement;
        let list = val[0].url.errorList;
        let errorStr = [];
        list.find(x=>{
          errorStr.push(h('p',null,x.errorMsg+"行数:"+x.rowNO));
        });
        this.$message({
          type: 'error',
          message: h('div',null,errorStr),
          position: 'bottom-right'
        });
      }else if(val&&val!=1&&val.length>0){
        this.$message({
          type: 'success',
          message: '成功'
        });
      }
    },
    closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else{
        this.$emit('close',this.file)
      }

    },
  },
}

</script>
